package com.longdaji.decoration.utils;

import android.content.SharedPreferences;

import com.longdaji.decoration.Constants;
import com.longdaji.decoration.DecorationApplication;

import static android.content.Context.MODE_PRIVATE;

/**
 * Created by longtao on 2018/6/2.
 */
public class PreferencesUtil {

    public static void saveUserAccount(String account) {
        saveString(Constants.AUTO_LOGIN_USERNAME, account);
    }

    public static String getUserAccount() {
        return getString(Constants.AUTO_LOGIN_USERNAME);
    }

    public static void saveUserToken(String token) {
        saveString(Constants.KEY_USER_TOKEN, token);
    }

    public static String getUserToken() {
        return getString(Constants.KEY_USER_TOKEN);
    }

    public static void saveFirstLogin(boolean isFirst) {
        saveBoolean(Constants.KEY_FIRST_LOGIN, false);
    }

    public static boolean getFirstLogin() {
        return getBoolean(Constants.KEY_FIRST_LOGIN);
    }

    public static void saveUserPassword(String password) {
        saveString(Constants.AUTO_LOGIN_PASSWORD, password);
    }

    public static String getUserPassword() {
        return getString(Constants.AUTO_LOGIN_PASSWORD);
    }

    private static void saveString(String key, String value) {
        SharedPreferences.Editor editor = getSharedPreferences().edit();
        editor.putString(key, value);
        editor.commit();
    }

    private static String getString(String key) {
        return getSharedPreferences().getString(key, null);
    }

    private static void saveBoolean(String key, boolean value) {
        SharedPreferences.Editor editor = getSharedPreferences().edit();
        editor.putBoolean(key, value);
        editor.commit();
    }

    private static boolean getBoolean(String key) {
        return getSharedPreferences().getBoolean(key, true);
    }


    static SharedPreferences getSharedPreferences() {
        return DecorationApplication.getInstance().getSharedPreferences(Constants.TAO_XIN, MODE_PRIVATE);
    }
}
